package com.founder.android.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

public class AsynchronousButton extends FrameLayout {
	
	private ProgressBar mProgress;
	
	private Button mButton;

	public AsynchronousButton(Context context) {
		super(context);
		init();
	}
	
	public AsynchronousButton(Context context, 
			AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public AsynchronousButton(Context context, 
			AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	@Override
	public void setEnabled(boolean enabled) {
		if (enabled) {
			mProgress.setVisibility(View.GONE);
			mButton.setVisibility(View.VISIBLE);
		} else {
			mProgress.setVisibility(View.VISIBLE);
			mButton.setVisibility(View.GONE);
		}
		super.setEnabled(enabled);
	}
	
	private void init() {
		mProgress = new ProgressBar(getContext());
		addView(mProgress, new FrameLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, 
				LayoutParams.WRAP_CONTENT,
				Gravity.CENTER));
		
		mButton = new Button(getContext());
		mButton.setTextSize(16);
		mButton.setTypeface(Typeface.DEFAULT_BOLD);
		mButton.setTextColor(Color.WHITE);
		addView(mButton, new FrameLayout.LayoutParams(
				LayoutParams.FILL_PARENT, 
				LayoutParams.FILL_PARENT));
		setEnabled(true);
	}

	public void setText(String text) {
		mButton.setText(text);
	}

	@Override
	public void setOnClickListener(OnClickListener l) {
		mButton.setOnClickListener(l);
	}

}
